﻿var sourcemap={
	city:{sx:0,sy:0,w:288,h:512},
	floor:{sx:584,sy:0,w:336,h:111},
	bird0:{sx:6,sy:982,w:36,h:24},
	bird1:{sx:62,sy:982,w:36,h:24},
	bird2:{sx:118,sy:982,w:36,h:24},
	tpiepline:{sx:112,sy:646,w:52,h:320},
	bpiepline:{sx:168,sy:646,w:52,h:320},
	end:{sx:791,sy:118,w:190,h:42}
	};
	
var starGame=function()
{
	//alert(window.innerHeight+""+window.innerWidth);
	Bird.init();
	Game.setupMoubile();
	Game.setboards(Background,0);	
	Game.setboards(Bird,1);	
	Game.setboards(Piepline,2);	
	Game.setupInput();
	Game.loop();
}

window.addEventListener("load", function(){Source.load(starGame);},false);

var Background=new function()
{
	this.citymap=sourcemap['city'];
	this.floormap=sourcemap['floor'];
	
	this.draw=function(ctx)
	{
		Source.draw(ctx,this.citymap,0,0);
		this.floorDraw(ctx,0,Game.height-111);
	}
	
	this.step=function(dt)
	{
		offset=this.intoffset+dt*100;
		this.intoffset=Math.floor(offset);
		this.intoffset=this.intoffset%336;
	}
	
	this.intoffset=0;
	this.floorDraw=function(ctx,x,y)
	{
		remain=336-this.intoffset;
		var temp={};
		for(var key in this.floormap)
		{
			temp[key]=this.floormap[key];	
		}
		if(this.intoffset>0)
		{
			temp.w=this.intoffset;
			Source.draw(ctx,temp,x+remain,y);	
		}	
		if(remain>0)
		{
			temp.w=remain;
			temp.sx=this.floormap.sx+this.intoffset;
			Source.draw(ctx,temp,x,y);	
		}
	}
}

var Bird=new function()
{
	this.init=function()
	{
		this.tx=Game.width/4; //ctx在Game.ctx的位置
		this.ty=Game.height/2-sourcemap['bird0'].h;
		
		this.frame=0; //bird帧控制
		this.rotate=0;  //转动量
		this.trotate=0;  //总转动量
		this.vy=0;    //垂直速度
		this.vr=0;    //转动速度
		this.ay=10;   //垂直加速度
		this.ar=25;   //转动加速度
		
	
		//this.ctx.translate(25,25); //重置中心
		
	}
	
	
	this.draw=function(ctx)
	{
		var temp;
		if(this.frame<3) 
			temp=sourcemap['bird0'];
		if(this.frame>=3 && this.frame<6) 
			temp=sourcemap['bird1']; 
		if(this.frame>=6) 
			temp=sourcemap['bird2']; 
	
		ctx.save();
		ctx.translate(this.tx+temp.w/2, this.ty+temp.h/2);
		if( (this.rotate>0 && this.trotate<=90)  )  //顺时针，判断是否转到90度
		{
			if(this.trotate+this.rotate>90) this.rotate=90-this.trotate; //判断是否会超过90度
			this.trotate+=this.rotate;
			ctx.rotate(this.trotate*Math.PI/180);
			
		}
		else if(this.rotate<0 && this.trotate>=-15)  //逆时针，判断是否转到-15度
		{
			if(this.trotate+this.rotate<-15) this.rotate=-15-this.trotate; //当前转动是否会超过-15度
			this.trotate+=this.rotate;
			ctx.rotate(this.trotate*Math.PI/180);
		}
		Source.draw(ctx,temp,-temp.w/2,-temp.h/2);
		ctx.translate(0,0);
		ctx.restore();
	}	
	
	this.step=function(dt)
	{
		this.frame++;
		this.frame%=10;
		var t=dt*10;  //10倍时间
		if(Game.touch)
		{
			this.vy=-25;	
			this.vr=-90;
			Game.touch=0;
		}
		
		this.ty+=this.vy*t+0.5*this.ay*t*t;
		this.vy+=this.ay*t
		
		this.rotate=this.vr*t+0.5*this.ar*t*t;
		this.vr+=this.ar*t;
		
		this.ty=Math.floor(this.ty);	
	}
}

var Piepline = new function()
{
	this.tmap=sourcemap['tpiepline'];
	this.bmap=sourcemap['bpiepline'];
	this.ttemp={};  //上水管包复制
	this.btemp={};  //下水管包复制
	this.dx=100;    //水管左右间隔
	this.dh=80;     //水管上下间隔
	this.max=3;     //水管数量
	this.w=this.tmap.w; //水管宽度
	this.totalheight=Game.height-sourcemap['floor'].h; //去地板剩下的空间
	this.top=[];       //上水管长度数组
	this.bottom=[];    //下水管长度数组
	this.x=Game.width+100;  //水管列最左端坐标
	
	for(var i=1; i<=this.max; i++) //生成随机水管序列
	{
		htop=Math.floor(Math.random()*(this.totalheight-180) )+this.dh; 
		hbottom=this.totalheight-htop;
		if(htop > hbottom)
			{htop-=this.dh; }
		else 
			{hbottom-=this.dh;}
		this.top[i]=htop;
		this.bottom[i]=hbottom;
	}
	
	for(var key in this.tmap )
	{
		this.ttemp[key]=this.tmap[key];	
	}
	
	for(var key in this.bmap )
	{
		this.btemp[key]=this.bmap[key];	
	}
	
	this.step=function(dt)
	{
		this.x-=dt*100;	
	}
	
	this.draw=function(ctx)
	{
		this.nl=Math.floor( (-this.x-this.w)/(this.dx+this.w)+1 )+1;
		this.nr=Math.floor( (Game.width-this.x)/(this.dx+this.w)+1 )+1;
		
		if(this.nl<1) this.nl=1;
		
		for(var i=this.nl; i<=this.nr && this.nr>=1; i++)
		{
			x=(i-1)*(this.dx+this.w)+this.x; //第i个水管x坐标
			x=Math.floor(x);
			d=this.tmap.h-this.top[i];
			this.ttemp.sy=this.tmap.sy+d;
			this.ttemp.h=this.top[i];
			y=0;
			Source.draw(ctx,this.ttemp,x,y);
			
			d=this.bmap.h-this.bottom[i];
			this.btemp.h=this.bottom[i];
			Source.draw(ctx,this.btemp,x,this.ttemp.h+this.dh);
		}
	}
}